import Vue from 'vue'
import Router from 'vue-router'
import Admin from '../components/admin/Admin'
import UserAdd from '../components/admin/User_add'
import UserList from '../components/admin/User_list'
import Head from '../components/base/Head'
import Article from '../components/page/Article'
import Index from '../components/page/Index'
import Regist from '../components/login/Regist'
import Calculate from '../components/forum/Calculate'

import AddArticle from '../components/page/AddArticle'
import IndexContentHot from '../components/page/IndexContentHot'
import IndexContentFollow from '../components/page/IndexContentFollow'
import UserInfo from '../components/forum/user/UserInfo'
import Login from '../components/forum/Login'

import ArticleList from '../components/admin/Article_list'
import CommentList from '../components/admin/Comment_list'
import ErrorLogin from '../components/admin/Error'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/errorLogin',
      name: 'error',
      component: ErrorLogin
    },
    {
      path: '/regist',
      name: 'Regist',
      component: Regist
    },
    {
      path: '/admin',
      name: 'Admin',
      component: Admin,
      children: [
        {
          path: '/userAdd',
          name: 'UserAdd',
          component: UserAdd
        },
        {
          path: '/userList',
          name: 'UserList',
          component: UserList
        },
        {
          path: '/articleList',
          name: 'ArticleList',
          component: ArticleList
        },
        {
          path: '/commentList',
          name: 'CommentList',
          component: CommentList
        }
      ]
    },
    {
      path: '/head',
      name: 'head',
      component: Head
    },
    {
      path: '/index',
      component: Index,
      children: [
        {
          path: 'indexContent/hot',
          component: IndexContentHot
        },
        {
          path: 'indexContent/follow',
          component: IndexContentFollow,
          meta: {
            require: true
          }
        },
        {
          path: 'article',
          component: Article
        },
        {
          path: 'addArticle',
          name: 'addArticle',
          component: AddArticle,
          meta: {
            require: true
          }
        },
        {
          path: 'userInfo',
          component: UserInfo,
          meta: {
            require: true
          }
        }
      ]
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      redirect: '/index/indexContent/hot'
    },
    {
      path: '/calculate',
      name: 'Calculate',
      component: Calculate
    }
  ]
})
var vue = new Vue()
// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.meta.require) {
    // 需要登录页面
    vue.axios.get('/user/getUser').then(function (res) {
      if (res.data.result) {
        // 已登录
        next()
      } else {
        // 未登录
        next('/login')
      }
    })
  } else {
    // 不需要登录页面
    next()
  }
})

export default router
